package com.heatup.apt.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.heatup.apt.management.service.access.AccessService;
import com.heatup.apt.management.service.store.StoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 门禁密码管理
 * Created by Blank on 2016-11-2.
 */
@Controller
@RequestMapping("/system/accessPwd")
public class AccessController extends BaseController {
    public static final Logger log = LoggerFactory.getLogger(AccessController.class);

    @Autowired
    AccessService accessService;

    @Autowired
    StoreService storeService;


    /**
     * 门禁密码列表页
     *
     * @return
     */
    @RequestMapping(value = "/to_access_pwd_list")
    public ModelAndView toAccessPwdList() {
        ModelAndView modelAndView = new ModelAndView("access/access-pwd-list");
        modelAndView.addObject("storeList", storeService.queryAllStore());
        return modelAndView;
    }


    /**
     * 订单门禁密码列表分页查询
     *
     * @param response
     * @return
     */
    @RequestMapping(value = "/access_pwd_list")
    public void accessPwdList(HttpServletResponse response,
                              @RequestParam int pageIndex,
                              @RequestParam int pageSize,
                              @RequestParam int relationType,
                              Integer relationId,
                              Integer userId,
                              String mobile,
                              String orderNumber,
                              Integer storeId,
                              Date startTime,
                              Date endTime
    ) {

        if (pageIndex > 0)
            --pageIndex;
        if (pageSize <= 0)
            pageSize = 10;
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("pageIndex", pageIndex);
        paramsMap.put("pageSize", pageSize);
        paramsMap.put("relationId", relationId);//订单ID//教练ID/系统用户ID
        paramsMap.put("userId", userId);//用户ID
        paramsMap.put("mobile", mobile);//用户手机/教练手机/系统用户手机
        paramsMap.put("orderNumber", orderNumber);
        paramsMap.put("storeId", storeId);
        paramsMap.put("startTime", startTime);
        paramsMap.put("endTime", endTime);
        paramsMap.put("relationType", relationType);//密码类型 0订单 1教练 2系统用户
        Map<String, Object> map = accessService.queryAccessPwdList(paramsMap);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accessPwdList", map.get("accessPwdList"));
        jsonObject.put("accessPwdCount", map.get("accessPwdCount"));
        responseSendMsg(response, jsonObject.toString());
    }
}
